/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.spareparts.material.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.spareparts.material.entity.SparepartsOutLibraryRegistration;
import com.jeeplus.modules.spareparts.material.service.SparepartsOutLibraryRegistrationService;

/**
 * 出库单登记流程Controller
 * @author 刘明星
 * @version 2020-10-16
 */
@RestController
@RequestMapping(value = "/spareparts/material/sparepartsOutLibraryRegistration")
public class SparepartsOutLibraryRegistrationController extends BaseController {

	@Autowired
	private SparepartsOutLibraryRegistrationService sparepartsOutLibraryRegistrationService;

	@ModelAttribute
	public SparepartsOutLibraryRegistration get(@RequestParam(required=false) String id) {
		SparepartsOutLibraryRegistration entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sparepartsOutLibraryRegistrationService.get(id);
		}
		if (entity == null){
			entity = new SparepartsOutLibraryRegistration();
		}
		return entity;
	}

	/**
	 * 出库单登记列表数据
	 */
	@GetMapping("list")
	public AjaxJson list(SparepartsOutLibraryRegistration sparepartsOutLibraryRegistration, HttpServletRequest request, HttpServletResponse response) {
		Page<SparepartsOutLibraryRegistration> page = sparepartsOutLibraryRegistrationService.findPage(new Page<SparepartsOutLibraryRegistration>(request, response), sparepartsOutLibraryRegistration);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取出库单登记数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(SparepartsOutLibraryRegistration sparepartsOutLibraryRegistration) {
		return AjaxJson.success().put("sparepartsOutLibraryRegistration", sparepartsOutLibraryRegistration);
	}

	/**
	 * 保存出库单登记
	 */
	@PostMapping("save")
	public AjaxJson save(SparepartsOutLibraryRegistration sparepartsOutLibraryRegistration, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(sparepartsOutLibraryRegistration);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑保存
		sparepartsOutLibraryRegistrationService.save(sparepartsOutLibraryRegistration);//保存
		return AjaxJson.success("保存出库单登记成功").put("businessTable", "spareparts_out_library_registration").put("businessId", sparepartsOutLibraryRegistration.getId());
	}


	/**
	 * 批量删除出库单登记
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			sparepartsOutLibraryRegistrationService.delete(new SparepartsOutLibraryRegistration(id));
		}
		return AjaxJson.success("删除出库单登记成功");
	}


}